//
//  JMAppNetworkClient.m
//  JMDome_1
//
//  Created by ios on 15/6/12.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMAppNetworkClient.h"
#import "JMConfigure.h"
#import "JMHeader.h"

@interface JMAppNetworkClient ()


@property (nonatomic, strong) JMConfigure * currConfigure;

@end

@implementation JMAppNetworkClient
@synthesize manager = _manager;

#pragma mark chu shi hua xia zai shu ju de xiang gaun fangfa
+ (instancetype) shareAppNetworkingclient{
    static  JMAppNetworkClient * shareClient = nil;
    static dispatch_once_t priedicate;
    dispatch_once(&priedicate, ^{
        shareClient = [[self alloc]init];
    });
    return shareClient;
}

-(AFHTTPRequestOperationManager *)manager{
    static AFHTTPRequestOperationManager * manager = nil;
    static dispatch_once_t priedicate;
    dispatch_once(&priedicate, ^{
        manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:HOST_URL]];
        manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        
        manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject: @"text/html"];
    });
    return _manager = manager;
}
- (JMConfigure *)currConfigure{
    if (!_currConfigure) {
        _currConfigure = [JMConfigure sharedConfigureManger];
    }
    return _currConfigure;
}


+ (NSMutableDictionary *)getCommonParametsForSever{
    NSMutableDictionary *parametes = [[NSMutableDictionary alloc]initWithCapacity:0];
    [parametes setObject:[[self alloc]currConfigure].deviceString forKeyedSubscript:@"device"];
    [parametes setObject:[[self alloc]currConfigure].signString forKeyedSubscript:@"sign"];
    [parametes setObject:[[self alloc]currConfigure].projectID forKeyedSubscript:@"appid"];
    return parametes;
}

#pragma mark 相关下载数据的主体

+ (AFHTTPRequestOperation *) interFaceWithInfo:(NSDictionary *) parametes compelete:(JMAPPNetworkingBlock)block{
    
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:InterFace parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);
        
        block(responseObject,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
    }];
}


/**
 登录接口
 **/
+ (AFHTTPRequestOperation *) postLoginWithInfo:(NSDictionary *) parametes compelete:(JMAPPNetworkingBlock)block{
    
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:LOGIN_URL_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);
        
        block(responseObject,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
    }];
}
/**
 注册接口 返回数据 
 **/
+ (AFHTTPRequestOperation *) postRegistWithInfo:(NSDictionary *) parametes compelete:(JMAPPNetworkingBlock)block{
    
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:REGIST_URL_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        block(responseObject,nil);
        NSLog(@"TTT::%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}
/**
 * 退出登录
 **/

+ (AFHTTPRequestOperation *)signOutWithINfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:SINGOUT_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}


+ (AFHTTPRequestOperation *)getBackPwdWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock )block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:GETBACKPWD_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}

+ (AFHTTPRequestOperation *)getUserInfoWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:USERINFO_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];

}

+ (AFHTTPRequestOperation *)alterUserInfoWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:ALTER_USERINFO_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}

/**
 * 我关注的学校
 */
+ (AFHTTPRequestOperation *)concernSchollListWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:CONCERN_SCHOOL_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}

/**
 * 我关注的专业
 */

+ (AFHTTPRequestOperation *)concernProfessListWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:CONCERN_PREFESS_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}


/**
 *学习圈
 */
+ (AFHTTPRequestOperation *)studyCircleListWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:STUDYCIRCLE_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        block(nil,error);
    }];
}
/**
 * 招生院校 recruitStudentSacademy
 */
+ (AFHTTPRequestOperation *)recruitStudentSacademyListWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    
    NSLog(@"WW::%@",dict);
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:ACADEM_API parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"EEWW::%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}
+ (AFHTTPRequestOperation *)SchoolIfoWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:School_Info_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}

/**
 * 获得专业列表
 */
+ (AFHTTPRequestOperation *)getProfessionalSettingListWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:ProfessionalSetting_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}
/**
 *招生政策
 */
+ (AFHTTPRequestOperation *)enrollmentPolicyListWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:EnrollmentPolicy parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}



/**
 * 添加关注学校
 */
+ (AFHTTPRequestOperation *) addSchool_AttentionWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:Add_School_Attention_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];

}

/**
 *取消关注学校
 */
+ (AFHTTPRequestOperation *) cancleSchool_AttentionWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:Cancle_School_Attention_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}
/**
 *添加关注专业
 */
+ (AFHTTPRequestOperation *) AddProfessional_AttentionWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:Add_Professional_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}
/**取消关注专业
 *
 */
+ (AFHTTPRequestOperation *) CancleProfessional_AttentionWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:Cancle_Professional_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}

#pragma mark  ======================================
/**
 * 上传 题目
 **/
+ (AFHTTPRequestOperation *) circleUpQuestionWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    
    
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:Circle_UpQuestion_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

/**
 * 回答 题目
 **/

+ (AFHTTPRequestOperation *) circle_AnswerQuestionWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:Circle_AnswerQuestion_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@",responseObject);

        block(responseObject,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}

/**
 * 回答 历史
 **/

+ (AFHTTPRequestOperation *) circle_history_AnswerWithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:Circle_history_Answer_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
        NSLog(@"%@",responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
        NSLog(@"RRR::%@",operation);
    }];
}


+ (AFHTTPRequestOperation *) token_WithInfo:(NSDictionary *)parametes complete:(JMAPPNetworkingBlock)block{
    NSMutableDictionary * dict = [self getCommonParametsForSever];
    [dict addEntriesFromDictionary:parametes];
    return [[[JMAppNetworkClient shareAppNetworkingclient] manager] POST:tokenIntFace_api parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        block(responseObject,nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error){
        block(nil,error);
    }];
}


///////////////////////////////////////////////////////////////////////////
+(NSDictionary *)userKey{
    NSMutableDictionary *parametes = [[NSMutableDictionary alloc]initWithCapacity:0];
    [parametes setObject:[[self alloc]currConfigure].projectID forKeyedSubscript:@"appid"];
    
    CGFloat scale_screen = [UIScreen mainScreen].scale;
    [parametes setObject:[NSString stringWithFormat:@"%f",UI_SCREEN_WITH] forKeyedSubscript:@"width"];
    [parametes setObject:[NSString stringWithFormat:@"%f",UI_SCREEN_WITH *scale_screen] forKeyedSubscript:@"pex"];
    JMConfigure *cf = [JMConfigure sharedConfigureManger];
    if(cf.user){
         [parametes setObject:cf.user.userId forKeyedSubscript:@"user"];
         [parametes setObject:cf.user.token forKeyedSubscript:@"token"];
         [parametes setObject:@"ios" forKeyedSubscript:@"flag"];
    }
    return parametes;
}

+ (NSString *)urlString:(NSDictionary *)parameters baseUrl:(NSString *)str{
    NSArray *array = [parameters allKeys];
    
    str= [str stringByAppendingString:@"?"];
    for(int i = 0;i <array.count;i++){
        NSString *u =[NSString stringWithFormat:@"%@=%@",array[i],parameters[array[i]]];
        if(i != array.count - 1){
            u = [u stringByAppendingString:@"&"];
        }
        str = [str stringByAppendingString:u];
    }
    NSLog(@"%@",str);
    return str;
}
+ (NSString *)independentCandidatesAnswerUrl{
    
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,independentCandidatesAnswer];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}

+ (NSString *)boostTheArtsUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
     NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,boostTheArts];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}
+ (NSString *)artExamineeAnswerUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,artExamineeAnswer];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}
+ (NSString *)trainingCampUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,trainingCamp];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}

+ (NSString *)summerCampTourUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,summerCampTour];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}
+ (NSString *)summerCampQuestionAnswerUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,summerCampQuestionAnswer];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}

+ (NSString *)whereTheInterestUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,whereTheInterest];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}
+ (NSString *)inspirationalBigCoffeeUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,inspirationalBigCoffee];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}
+ (NSString *)awarInTheEndUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,awarInTheEnd];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}

+ (NSString *)aboutUsUrl{
    NSDictionary *parameters = [JMAppNetworkClient userKey];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,about_us];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}
+ (NSString *)xinggeceshiResultUrl:(NSDictionary *)dict{
    NSMutableDictionary *parameters = (NSMutableDictionary *)[JMAppNetworkClient userKey];
    [parameters addEntriesFromDictionary:dict];
    NSString *str = [NSString stringWithFormat:@"%@%@",HOST_URL,xingge_result];
    return [JMAppNetworkClient urlString:parameters baseUrl:str];
}

@end
